function setMsg(msg){
	chrome.extension.getBackgroundPage().msg = msg;
}

function getMsg(){
	return chrome.extension.getBackgroundPage().msg;
}

function showMsg(msg){
	if(msg){
		$(".lastmenuitem").removeClass("noborder");
		$("#info").html(msg);
	} else {
		$(".lastmenuitem").addClass("noborder");
		$("#info").html("");
	}
}

function setState(state){
	chrome.extension.getBackgroundPage().state = state;
}
function getState(){
	return chrome.extension.getBackgroundPage().state;
}

function disableMenus(){
	$("#menu").css('display', 'none');
}

function enableMenus(){
	$("#menu").css('display', 'block');
}

function execute(elem){
	var cmd = elem.id;
	if(!chrome.extension.getBackgroundPage().hasAccountBeenSet()){
		window.close();
		return;
	}
	setState("busy");
	// $(".menuitem").addClass("menudisabled").attr('onClick', '');
	disableMenus();
	chrome.extension.sendRequest({'cmd' : cmd});
	window.close();
}
function loadMenu(){
	$(".menuitem").each(function(){
		$(this).html(chrome.i18n.getMessage("btn_" + this.id));
	});
}

document.addEventListener('DOMContentLoaded', function(){
	loadMenu();
	if(chrome.extension.getBackgroundPage().hasAccountBeenSet()){
		if(getState() == "idle"){
			enableMenus();
		} else if(getState() == "busy"){
			showMsg(chrome.i18n.getMessage("info_inprogress"));
		}
	} else {
		showMsg(chrome.i18n.getMessage("err_accountNotSet", chrome.extension.getURL('options.html')));
	}
	$(".menuitem").click(function(event){
		execute(event.currentTarget);
	});
});